package com.lzb.musicplayer.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.blankj.utilcode.util.UriUtils;
import com.bumptech.glide.Glide;
import com.lzb.basic.utils.SPUtil;
import com.lzb.basic.utils.UriUtil;
import com.lzb.musicplayer.BaseAc;
import com.lzb.musicplayer.Content;
import com.lzb.musicplayer.R;
import com.lzb.musicplayer.databinding.ActivityInterfaceBinding;
import com.lzb.musicplayer.BaseApplication;
import com.lzb.musicplayer.utils.BackgroundUtil;

public class InterfaceActivity extends BaseAc<ActivityInterfaceBinding> implements View.OnClickListener {

    private String imgPath;
    private static final int REQUEST_IMAGE_PICK = 1001;

    @Override
    protected int onCreate() {
        return R.layout.activity_interface;
    }

    @Override
    protected void initView() {
        mDataBinding.ivInterfaceBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.ivImg.setOnClickListener(this);
        mDataBinding.tvChange.setOnClickListener(this);
        mDataBinding.tvSave.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.ivImg) {//图片
            if (TextUtils.isEmpty(imgPath)) {
                openImagePicker();
            }
        } else if (id == R.id.tvChange) {//重置背景
            openImagePicker();
        } else if (id == R.id.tvSave) {//保存并应用
            SPUtil.putString(mContext, Content.backgroundPath, imgPath);
            mDataBinding.tvSave.setVisibility(View.GONE);
            mDataBinding.tvChange.setVisibility(View.VISIBLE);

            // 保存后应用背景图片到当前Activity
            if (!TextUtils.isEmpty(imgPath)) {
                BackgroundUtil.setActivityBackground(this, imgPath);
                Toast.makeText(mContext, "背景图片已应用", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void initData() {
        imgPath = SPUtil.getString(mContext, Content.backgroundPath, "");
        if (!TextUtils.isEmpty(imgPath)) {
            Glide.with(mContext).load(imgPath).into(mDataBinding.ivImg);
            mDataBinding.tvChange.setVisibility(View.VISIBLE);

            // 应用保存的背景图片到当前Activity
            BackgroundUtil.applySavedBackground(this);
        }
    }


    // 图片选择相关方法
    private void openImagePicker() {
//        BaseApp.isImagePickerActive = true;
        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        intent.setType("image/*");
        startActivityForResult(intent, REQUEST_IMAGE_PICK);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_IMAGE_PICK && resultCode == RESULT_OK && data != null) {
            Uri selectedImageUri = data.getData();
            if (selectedImageUri != null) {
                imgPath = selectedImageUri.toString();
                Glide.with(mContext).load(Uri.parse(imgPath)).into(mDataBinding.ivImg);
                mDataBinding.tvSave.setVisibility(View.VISIBLE);
                mDataBinding.tvChange.setVisibility(View.GONE);
            }
        }
    }

}